unit VDGO_RepEditFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AppUtils, BaseForm, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, dxSkinsCore, dxSkinOffice2010Blue, dxSkinscxPCPainter,
  cxPropertiesStore, dxLayoutLookAndFeels, dxLayoutContainer, dxLayoutControl,
  dxSkinDevExpressStyle, dxSkinVS2010, cxContainer, cxEdit,
  dxLayoutcxEditAdapters, dxLayoutControlAdapters, Vcl.Menus, Vcl.StdCtrls,
  cxButtons, cxMemo, cxDropDownEdit, cxTextEdit, cxMaskEdit, cxLookupEdit,
  cxDBLookupEdit, cxDBLookupComboBox, KontragentRepository, Data.DB, FIBDataSet,
  pFIBDataSet, cxClasses;

type
  TVDGO_RepEditForm = class(TDezForm)
    grp1: TdxLayoutGroup;
    lc1: TdxLayoutControl;
    lf1: TdxLayoutLookAndFeelList;
    lcf1: TdxLayoutCxLookAndFeel;
    prop1: TcxPropertiesStore;
    lGrplc1Group1: TdxLayoutGroup;
    lGrplc1Group2: TdxLayoutGroup;
    cbb_SrvDog: TcxLookupComboBox;
    litm_lc1Item1: TdxLayoutItem;
    cbb_Kontr: TcxLookupComboBox;
    litm_lc1Item11: TdxLayoutItem;
    cbb_Y: TcxComboBox;
    litm_lc1Item12: TdxLayoutItem;
    m_Note: TcxMemo;
    litm_lc1Item13: TdxLayoutItem;
    btn_OK: TcxButton;
    litm_lc1Item14: TdxLayoutItem;
    btn_Cancel: TcxButton;
    litm_lc1Item15: TdxLayoutItem;
    src_kontr: TDataSource;
    src_SrvDog: TDataSource;
    ds_SrvDog: TpFIBDataSet;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure cbb_KontrPropertiesEditValueChanged(Sender: TObject);
    procedure cbb_SrvDogPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure cbb_YPropertiesValidate(Sender: TObject;
      var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
    procedure btn_OKClick(Sender: TObject);
  private
    { Private declarations }
    fController: TKontragentController;
    procedure ReopenSrvDog(kontrId: Variant);
    function Validate: Boolean;
  public
    { Public declarations }
  end;

var
  VDGO_RepEditForm: TVDGO_RepEditForm;

implementation

uses cmDBUnit;

{$R *.dfm}


procedure TVDGO_RepEditForm.btn_OKClick(Sender: TObject);
begin
  if Validate then
    ModalResult:= mrOk;
end;

procedure TVDGO_RepEditForm.cbb_KontrPropertiesEditValueChanged(
  Sender: TObject);
begin
  ReopenSrvDog(cbb_kontr.EditValue);
end;

procedure TVDGO_RepEditForm.cbb_SrvDogPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
  Error:= VarIsType((Sender as TcxLookupComboBox).EditValue,[varNull, varEmpty]);
end;

procedure TVDGO_RepEditForm.cbb_YPropertiesValidate(Sender: TObject;
  var DisplayValue: Variant; var ErrorText: TCaption; var Error: Boolean);
begin
    Error:= VarIsType(DisplayValue,[varNull, varEmpty]);
end;

procedure TVDGO_RepEditForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  //Action:= caFree;
  fController.Free;
end;

procedure TVDGO_RepEditForm.FormCreate(Sender: TObject);
begin
  LoadForm(prop1,Self.Name);
  fController:= TKontragentController.Create;
  src_kontr.DataSet:= fController.DS;
  fController.GetActiveKontrShort();
end;

procedure TVDGO_RepEditForm.ReopenSrvDog(kontrId: Variant);
begin
  with ds_SrvDog do begin
    Close;
    ParamByName('kontr').AsVariant:= kontrId;
    Open;
  end;
  if not ds_SrvDog.IsEmpty then
     cbb_srvDog.ItemIndex := 0;
end;

function TVDGO_RepEditForm.Validate: Boolean;
begin
  Result:= False;
  if cbb_SrvDog.ValidateEdit(False) and
     cbb_Y.ValidateEdit(false) then
  Result:= True;
end;

initialization
 RegisterClass(TVDGO_RepEditForm);
end.
